package xyx.chenweb.myapp.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xyx.chenweb.myapp.entity.UserInfo;

@Service
@CacheConfig(cacheNames = {"userCache", "infor"})
public class EhcacheService {

    private final String USERCACHE = "userCache";
    private final String INFOR = "infor";

    private static Logger logger = LoggerFactory.getLogger(EhcacheService.class);

    /**
     * 从缓存中删除用户信息
     * @param id
     */
    @CacheEvict(value = USERCACHE, key = "'user_'+#id")
    public void deleteUser(String id){
        logger.info("删除用户信息");
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @Cacheable(value = USERCACHE, key = "'user_'+#id")
    public UserInfo getUser(String id){
        logger.info("获取用户信息");
        return null;
    }

    /**
     * 保存用户信息
     * @param userInfo
     * @param id
     * @return
     */
    @CachePut(value = USERCACHE, key = "'user_'+#id")
    public UserInfo saveUser(UserInfo userInfo, String id){
        logger.info("保存用户信息");
        return userInfo;
    }

    /**
     * 从缓存中删除信息
     * @param id
     */
    @CacheEvict(value = INFOR, key = "'info:'+#id")
    public void deleteInfor(String id){
        logger.info("删除信息");
    }

    /**
     * 在缓存中添加信息
     *
     * @param infor
     * @param id
     * @return
     */
    @CachePut(value = INFOR, key = "'info:'+#id")
    public Object saveInfor(Object infor, String id){
        logger.info("保存信息");
        return infor;
    }

    /**
     * 获取信息
     *
     * @param id
     * @return
     */
    @Cacheable(value = INFOR, key = "'info:'+#id")
    public Object getInfor(String id){
        logger.info("获取信息");
        return null;
    }


}
